﻿using HR.Filters;
using HR.IServices.PengIService;
using HR.Models.PengModels;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Security.Policy;
using System.Threading;
using System.Xml.Linq;

namespace HR.Controllers
{
    public class RenLiZiYuanDangAnGuanLiController : Controller
    {
		private readonly IRenLiZiYuanGuanLiServices renLiGuanLiServices;
		private readonly ILogger<RenLiZiYuanDangAnGuanLiController> logger;
		private readonly IWebHostEnvironment webHostEnvironment;

		public RenLiZiYuanDangAnGuanLiController(IRenLiZiYuanGuanLiServices renLiGuanLiServices, ILogger<RenLiZiYuanDangAnGuanLiController> logger,IWebHostEnvironment webHostEnvironment)
		{
			this.renLiGuanLiServices = renLiGuanLiServices;
			this.logger = logger;
			this.webHostEnvironment = webHostEnvironment;
		}
		public async Task<IActionResult> Index(int id)
        {
			var list = await renLiGuanLiServices.JianLiID(id);
			ViewBag.Model = JsonConvert.SerializeObject(list);
			return View();
        }
        //查询薪酬
        public async Task<IActionResult> XinChou()
        {
            List<salary_standard> list = await renLiGuanLiServices.FindXinChou();
            return Json(list);
        }
        [HttpGet]
		public async Task<IActionResult> Add(RenLiZiYuanDangAnGuanLi record) 
		{
			int result = await renLiGuanLiServices.Add(record);
			if (result > 0)
			{
				return Content(result.ToString());
			}
			else 
			{
				return View(record);
			}
		}
		//自动生成编号
		public async Task<string> ZiDongID() 
		{
			string id = await renLiGuanLiServices.ZiDongID();
			return id;
		}
		//上传图片
		public IActionResult Img(IFormFileCollection file)
		{
			string path = "";
			foreach (var item in file)
			{
				string name = DateTime.Now.ToString("yyyyMMddHHmmssfff");
				string ext = item.FileName.Substring(item.FileName.LastIndexOf('.'));

				path = $"uploaders/" + name + ext;
				string jpath = Path.Combine(webHostEnvironment.WebRootPath, path);
				//(new FileInfo(jpath)).Directory.Create();
				using (FileStream filel = new FileStream(jpath, FileMode.CreateNew))
				{
					item.CopyTo(filel);
				}

			}
			return Json(path);
		}
		//上传文档
		public IActionResult WenDang(IFormFileCollection file)
		{
			string path = "";
			foreach (var item in file)
			{
				string name = DateTime.Now.ToString("yyyyMMddHHmmssfff");
				string ext = item.FileName.Substring(item.FileName.LastIndexOf('.'));
				path = $"File/" + name + ext;
				string jpath = Path.Combine(webHostEnvironment.WebRootPath, path);
				//(new FileInfo(jpath)).Directory.Create();
				using (FileStream filel = new FileStream(jpath, FileMode.CreateNew))
				{
					item.CopyTo(filel);
				}

			}
			return Json(path);
		}
		//登记复核页面
		public IActionResult DengJiFuHe() 
		{
			return View();
		}
		//登记复核分页
		[HttpGet]
		public async Task<IActionResult> FenYe(int size,int currentPage) 
		{
			FenYe<RenLiZiYuanDangAnGuanLi> list = await renLiGuanLiServices.FenYe(size,currentPage);
			return Json(list);
		}
		//登记复核页面修改传值
		public async Task<IActionResult> UpdateDengJiFuHe(int id) 
		{
			var list = await renLiGuanLiServices.FindId(id);
			ViewBag.Model = JsonConvert.SerializeObject(list).Replace(@"\\", "/");
			return View();
		}
		/// <summary>
		/// 这里是职工的复核事件，
		/// 1.复核通过后=>表示该职工已经正式入职。
		/// 2.需要将对应的薪酬信息添加到薪酬发放明细表中。
		/// </summary>
		/// <param name="renLiZiYuanDangAnGuanLi"></param>
		/// <returns></returns>
		[HttpGet]
		public async Task<IActionResult> UpdFuHe(RenLiZiYuanDangAnGuanLi renLiZiYuanDangAnGuanLi)
		{
			int result = await renLiGuanLiServices.Update(renLiZiYuanDangAnGuanLi);
			if (result > 0)
			{
				return Content(result.ToString());
			}
			else 
			{
				return View(result);
			}
		}
		[TypeFilter(typeof(RoleFilterAttribute))]
		//登记复核页面  查询
		public IActionResult FuHeYeMian()
		{
			return View();
		}
		[TypeFilter(typeof(RoleFilterAttribute))]
		//档案查询页面
		public IActionResult DangAnChaXun() 
		{
			return View();
		}
		//档案查询
		[HttpGet]
		public async Task<IActionResult> DangAnFind(string? one, string? two, string? three, string? name, string? type, string? minCheng, string? time1, string? time2) 
		{
			List<RenLiZiYuanDangAnGuanLi> list = await renLiGuanLiServices.DangAnFind(one,two,three,name,type,minCheng,time1,time2);
			return Json(list);
		}
		//查看页面
		[HttpGet]
		public async Task<IActionResult> ChaKanYeMian(int id) 
		{
			var list = await renLiGuanLiServices.FindId(id);
			ViewBag.Model = JsonConvert.SerializeObject(list).Replace(@"\\", "/");
			return View();
		}
		[TypeFilter(typeof(RoleFilterAttribute))]
		//档案变更页面
		public IActionResult DangAnBianGen() 
		{
			return View();
		}
		//档案变更分页
		[HttpGet]
		public async Task<IActionResult> FuHeFenYe(int size,int currentPage) 
		{
			FenYe<RenLiZiYuanDangAnGuanLi> list = await renLiGuanLiServices.FuHeFenYe(size,currentPage);
			return Json(list);
		}
		//变更操作页面
		public async Task<IActionResult> BianGenCaoZuo(int id) 
		{
			var list = await renLiGuanLiServices.FindId(id);
			string json = JsonConvert.SerializeObject(list).Replace(@"\r\n", "").Replace(@"\\", "/");
            ViewBag.Model = json;
            return View();
            //var list = await renLiGuanLiServices.JianLiID(id);
            //ViewBag.Model = JsonConvert.SerializeObject(list);
            //return View();
        }
		//变更修改
		[HttpGet]
		public async Task<IActionResult> UpdBianGen(RenLiZiYuanDangAnGuanLi renLiZiYuanDangAnGuanLi)
		{
			int result = await renLiGuanLiServices.UpdaBianGen(renLiZiYuanDangAnGuanLi);
			if (result > 0)
			{
				return Content(result.ToString());
			}
			else 
			{
				return View(result);
			}
		}
		[TypeFilter(typeof(RoleFilterAttribute))]
		//档案删除页面
		public IActionResult DangAnShanChu() 
		{
			return View();
		}
		//档案删除查询
		[HttpGet]
		public async Task<IActionResult> ShanChuFind(string? one, string? two, string? three, string? name, string? type, string? minCheng, string? time1, string? time2)
		{
			List<RenLiZiYuanDangAnGuanLi> list = await renLiGuanLiServices.ShanChuFind(one, two, three, name, type, minCheng, time1, time2);
			return Json(list);
		}
		//档案删除  修改状态
		[HttpGet]
		public async Task<IActionResult> UpdStatus(RenLiZiYuanDangAnGuanLi renLiZiYuanDangAnGuanLi) 
		{
			int result = await renLiGuanLiServices.UpdStatus(renLiZiYuanDangAnGuanLi);
			if (result > 0)
			{
				return Content(result.ToString());
			}
			else 
			{
				return View(result);
			}
		}
		[TypeFilter(typeof(RoleFilterAttribute))]
		//删除恢复页面
		public IActionResult ShanChuHuiFu() 
		{
			return View();
		}
		//恢复查询
		[HttpGet]
		public async Task<IActionResult> HuiFuFind(string? one, string? two, string? three, string? name, string? type, string? minCheng, string? time1, string? time2) 
		{
			List<RenLiZiYuanDangAnGuanLi> list = await renLiGuanLiServices.HuiFuFind(one, two, three, name, type, minCheng, time1, time2);
			return Json(list);
        }
		//恢复修改状态
		[HttpGet]
        public async Task<IActionResult> HuiFuStatus(RenLiZiYuanDangAnGuanLi renLiZiYuanDangAnGuanLi)
        {
            int result = await renLiGuanLiServices.HuiFuStatus(renLiZiYuanDangAnGuanLi);
            if (result > 0)
            {
                return Content(result.ToString());
            }
            else
            {
                return View(result);
            }
        }
		[TypeFilter(typeof(RoleFilterAttribute))]
		//永久删除页面
		public IActionResult YongJiuShanChu() 
		{
			return View();
		}
		//永久删除分页
		[HttpGet]
		public async Task<IActionResult> ShanChuFenYe(int size,int currentPage) 
		{
			FenYe<RenLiZiYuanDangAnGuanLi> list = await renLiGuanLiServices.ShanChuFenYe(size,currentPage);
			return Json(list);
		}
		//永久删除
		[HttpGet]
		public async Task<IActionResult> Delete(int huf_id) 
		{
			int result = await renLiGuanLiServices.Delete(huf_id);
			if (result > 0)
			{
				return Content(result.ToString());
			}
			else 
			{
				return View(result);
			}
		}
		[HttpGet]
		public async Task<IActionResult> SelQuanBu() {
			List<RenLiZiYuanDangAnGuanLi> list = await renLiGuanLiServices.SelQuanBu();
			return Json(list);
		}
    }
}
